from django.db import models
from barrelhouse.core.vendors.models import vendor_account, warranty

class license_type(models.Model):

    name = models.CharField(max_length=32, unique=True, help_text="name for this type of License")
    copyright = models.CharField(null=True, blank=True, max_length=32, help_text="copyright information for this license type")
    eula = models.URLField(null=True, blank=True, help_text="link to the online EULA, if available")
    notes = models.TextField(help_text="notes for this license type")

    def eula_admin(self):
        return "<a href=\"%s\">%s</a>" %(self.eula, self.eula)

    eula_admin.allow_tags = True

    class Meta:
        verbose_name = "License Type"

class system_type(models.Model):

    name = models.CharField(max_length=32, unique=True, db_index=True, help_text="The name of the distribution")
    release_number = models.CharField(max_length=6, help_text="major version number for an operating system, eg 5 for Redhat 5.3")
    version_number = models.CharField(max_length=6, help_text="minor version number for an operating system, eg 3 for Redhat 5.3")
    license_type = models.ForeignKey(license_type, help_text="type of license associated with this operating system")
    warranty = models.ForeignKey(warranty, null=True, blank=True, help_text="warranty for this software")

    class Meta:
        verbose_name = "System Type"

class operating_system(system_type):

    architecture = models.CharField(max_length=8, help_text="processor architecture for this operating system, eg x86_64")
    vendor = models.ForeignKey(vendor_account, null=True, blank=True, help_text="vendor that supports this OS")

    def get_full_version(self):
        return self.name + " " + self.release_number + "." + self.version_number + " " + self.architecture

    def full_version_admin(self):
        return get_full_version()

    full_version_admin.allow_tags = True

    def get_absolute_url(self):
        return "/operating_systems/%s" % self.id

    def __unicode__(self):
        return self.get_full_version()

    class Meta:
        verbose_name = "Operating System"

class firmware(system_type):

    vendor = models.ForeignKey(vendor_account, help_text="vendor that supplies or supports this firmware")

    def get_full_version(self):
        return self.name + " " + self.release_number + "." + self.version_number

    def full_version_admin(self):
        return get_full_version()

    full_version_admin.allow_tags = True

    def get_absolute_url(self):
        return "/firmware/%s" % self.id

    def __unicode__(self):
        return self.get_full_version()

    class Meta:
        verbose_name = "Firmware"
        verbose_name_plural = verbose_name

class software(models.Model):

    name = models.CharField(max_length=32, db_index=True, help_text="software name")
    operating_system = models.ForeignKey(operating_system, help_text="os this software is designed to run on")
    info = models.URLField(null=True, blank=True, verify_exists=False, help_text="a website containing information about this software")
    notes = models.TextField(help_text="notes for this piece of software")

    def __unicode__(self):
        return self.name

class licensed_software(software):

    key = models.CharField(max_length=512, unique=True,  help_text="License Key for this software purchase")

    def __unicode__(self):
        return self.name + " " + self.key

    def get_absolute_url(self):
        return "/licensed_software/%s" % self.id

    class Meta:
        verbose_name = "Licensed Software"
        verbose_name_plural = verbose_name

class opensource_software(software):

    license_type = models.ForeignKey(license_type, help_text="license this software is released under")

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "/opensource_software/%s" % self.id

    class Meta:
        verbose_name = "Opensource Software"
        verbose_name_plural = verbose_name
